<?php
	require_once("head.html");
?>
<body onload="drawCanvas()">
		<div id="wrapper">
			<header>
				<a href="index.php?logout=true">Log out</a>
			</header>
			<div id="content">
				
<?php
	mysql_connect("localhost", "web964","pass17") or die ("Connection failed.");
	mysql_select_db("usr_web964_1") or die ("Database does not extist.");
	
	$resultGoalInfo = mysql_query("SELECT * FROM goal WHERE id =".$_GET['id']) or die ("MySQL-Fehler: " . mysql_error());
	while($row1 = mysql_fetch_object($resultGoalInfo)){
		echo "<h1>Goal \"$row1->name\"</h1>";
		if ($row1->description != ""){
			echo "<p><b>Description: </b>$row1->description</p>";
		}else{
			echo "<p><b>Description: </b>-</p>";
		}
		
		$resultGoalDeadline = mysql_query("SELECT * FROM goaldeadline WHERE fk_goalid =".$_GET['id']) or die ("MySQL-Fehler: " . mysql_error());
		$goaldeadline = "";
		while($row2 = mysql_fetch_object($resultGoalDeadline)){
			$goaldeadline = $row2->date;
		}
		if($goaldeadline != ""){
			echo "<p><b>Deadline: </b>$goaldeadline</p>";
		}else{
			echo "<p><b>Deadline: </b>-</p>";
		}
		switch ($row1->priority) {
			case 'ui':
				echo "<p><b>Priority: </b>1 - urgent and important</p>";
				break;
			case 'uni':
				echo "<p><b>Priority: </b>2 - urgent and unimportant</p>";
				break;
			case 'nui':
				echo "<p><b>Priority: </b>3 - unurgent and important</p>";
				break;
			case 'nuni':
				echo "<p><b>Priority: </b>4 - unurgent and unimportant</p>";
				break;			
			default:
				echo "<p><b>Priority: </b>-</p>";
				break;
		}		
		
		$resultSubgoalsCount = mysql_query("SELECT id FROM subgoal WHERE fk_goalid =".$_GET['id'])or die ("MySQL-Fehler: " . mysql_error());
		$currentValueIfSubgoalsExists = 0;
		if (mysql_num_rows($resultSubgoalsCount) == 0){
			echo "<p><b>Start Value: </b>$row1->startvalue</p>";
			echo "<p><b>Current Value: </b>$row1->value</p>";
			echo "<p><b>Target Value: </b>$row1->targetvalue</p>";
			echo "<p><b>Measure Name: </b>$row1->measurename</p>";
			$canvasid = "canvas_goal_$row1->startvalue"."_$row1->targetvalue"."_$row1->value";
			echo "<p><b>Progress: </b><canvas id=\"$canvasid\" width=\"100\" height=\"10\"></canvas></p>";			
		}else{			
			echo "<p><b>Start Value: </b>0</p>";
			$currentValueIfSubgoalsExists = calculateCurrentValue($_GET['id']);
			echo "<p><b>Current Value: </b>".$currentValueIfSubgoalsExists."</p>";
			echo "<p><b>Target Value: </b>100</p>";
			echo "<p><b>Measure Name: </b>%</p>";
		$resultMeasureType = mysql_query("SELECT typename FROM measuretype WHERE id =".$row1->fk_measuretypeid)or die ("MySQL-Fehler: " . mysql_error());
		$measuretype = "";
		while($row3 = mysql_fetch_object($resultMeasureType)){
			$measuretype = $row3->typename;
		}
		echo "<p><b>Measure Type: </b>$measuretype</p>";
			$canvasid = "canvas_goal_0"."_100"."_$currentValueIfSubgoalsExists";
			echo "<p><b>Progress: </b><canvas id=\"$canvasid\" width=\"100\" height=\"10\"></canvas></p>";			
		}
		echo "<br/><hr/>";
	}	
			
	$erg = mysql_query("SELECT 
	subgoal.id, 
	subgoal.name, 
	subgoal.value, 
	subgoal.startvalue, 
	subgoal.targetvalue, 
	subgoal.measurename, 
	subgoal.priority, 
	measuretype.typename AS measuretypename
	 FROM subgoal, measuretype WHERE fk_goalid='".$_GET['id']."' AND measuretype.id=subgoal.fk_measuretypeid")
	or die ("MySQL-Fehler: " . mysql_error());
	
	if (mysql_num_rows($erg) == 0){
		echo "<br/>This goal has no subgoals.<br/><br/>";
	}else{
?>
	<table class="colored">
		<tr>
			<th>Subgoal Name</th>
			<th>Deadline</th>		
			<th>Start<br/>Value</th>
			<th>Value</th>
			<th>Target<br/>Value</th>
			<th>Measure<br/>Name</th>
			<th>Progress</th>
			<th>Percent</th>
			<th>Priority</th>
			<th>Measure<br/>Type</th>
		</tr>	
<?php						
		while($row = mysql_fetch_object($erg)){
			$subgoalid = $row->id;
			echo "<tr>";
			echo "<td>$row->name</a></td>";
			# get deadline
			$resultDeadline = mysql_query("SELECT date AS deadline FROM subgoaldeadline WHERE fk_subgoalid='$subgoalid'");
			if (mysql_num_rows($resultDeadline) == 0){
				echo "<td> - </td>";
			}else{
				while($row2 = mysql_fetch_object($resultDeadline)){
					echo "<td>$row2->deadline</td>";
				}
			}
			
			#echo "<td>$row->deadline</td>";		
			echo "<td>$row->startvalue</td>";
			echo "<td>$row->value</td>";
			echo "<td>$row->targetvalue</td>";
			echo "<td>$row->measurename</td>";
			
			#progress columns
			$canvasid = "canvas_".$subgoalid."_$row->startvalue"."_$row->targetvalue"."_$row->value";
			echo "<td><canvas id=\"$canvasid\" width=\"100\" height=\"10\"></canvas></td>";
			echo "<td></td>";
			
			
			echo "<td>$row->priority</td>";
			
			echo "<td>$row->measuretypename</td>";
			echo "</tr>";	
		}
			
		echo "</table>";
		echo "<br/><hr/>";
		
	}
?>					
					<br/>
					<a href="index.php?page=overview&back=1">goals overview</a>
				</div>
			<footer></footer>
		</div>
	</body>
	
<?php
	function calculateCurrentValue($goalid){ 
		mysql_connect("localhost", "web964","pass17") or die ("Connection failed.");
		mysql_select_db("usr_web964_1") or die ("Database does not exist.");
	
		$result = mysql_query("SELECT * FROM subgoal WHERE fk_goalid =".$goalid)or die ("MySQL-Fehler: " . mysql_error());
		$sumPersentage = 0;
		while($row = mysql_fetch_object($result)){
			
				$sumPersentage += computePercentage($row->startvalue, $row->targetvalue, $row->value);
			
		}		
		return number_format($sumPersentage / mysql_num_rows($result), 2);
	}
	
	function computePercentage($start, $target, $current){
		if($start == "no" && $target == "yes"){
			if ($current == "yes"){
				return 100;
			}
			return 0;
		}
		
		if ($target == $start){
			return 100;
		}
		return abs($current - $start) * 100 / abs($target - $start);
	}
?>